<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加账号</span>
      </div>
      <div class="text item">
        <el-form
          :model="addAccount"
          status-icon
          :rules="rules"
          ref="addAccount"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="账号" prop="account">
            <el-input
              v-model="addAccount.account"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="addAccount.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户组" prop="userGroup">
            <el-select
              v-model="addAccount.userGroup"
              placeholder="请选择用户组"
            >
              <el-option label="超级管理员" value="超级管理员"></el-option>
              <el-option label="普通管理员" value="普通管理员"></el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" @click="submitForm">添加</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import {validateAccount,validatePassword} from '@/utils/tools'
import {addAccount} from "@/api/user"
export default {
  name: "AccountAdd",
  data() {
    return {
      addAccount: {
        account: "",
        password: "",
        userGroup: "",
      },
      rules: {
         account:[ { required: true, validator: validateAccount, trigger: 'blur'},
           ],
        password:[ { required: true,validator: validatePassword, trigger: 'blur' },
           ],
        userGroup: [
          { required: true, message: "请选择用户组", trigger: "change" },
        ],
      },
    };
  },
  methods: {
   
    submitForm() {
      this.$refs.addAccount.validate(async (valid) => {
        if (valid) {
          console.log("添加成功");

          let res = await addAccount(this.addAccount)
          let {data} = res
          if(data.code==0){
               
        this.$router.push('/account/account-list');
          }
          
        } else {
          return false;
        }
      });
    },
   
    resetForm() {
       this.$refs.addAccount.resetFields()
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-input__inner {
  width: 197px;
}
</style>
